using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using MVCFramework.Models;
using System.Reflection;
using MVCFramework.Menus;

namespace MVCFramework.Presenter.States
{
    public interface IMenuHandlerContainer
    {
        ArrayList Menulist { get;set;}
        void DispathMenuEvent(IMenuCommand command);
        IList<IMenuCommand> GetCommands();
        void DispathMenuEvent(string command);
    }

    public abstract class MenuHandlerContainer : IMenuHandlerContainer
    {
        protected ArrayList menulist;
        public ArrayList Menulist
        {
            get
            {
                ParseMenu();
                return menulist;
            }
            set
            {
                menulist = value;
            }
        }  
        protected virtual void ParseMenu()
        {

        }

        #region IMenuHandlerContainer Members

        void IMenuHandlerContainer.DispathMenuEvent(IMenuCommand command)
        {
             command.Execute();
        }

        void IMenuHandlerContainer.DispathMenuEvent(string  command)
        {
            try
            {
                MethodInfo[] methods = this.GetType().GetMethods();
                foreach (MethodInfo info in methods)
                    if (info.Name.Equals(command + "Handler"))
                        info.Invoke(this, new Object[0]);
            }
            catch (Exception e) { throw new ModelExeption("Menu EventHandler could not be Invoked ", e); } 

        }

        #endregion

        #region IMenuHandlerContainer Members

        public abstract IList<IMenuCommand> GetCommands();

        #endregion
    }
    
}
